package com.dream.wukong.configcenter.client.remote.http;

import com.dream.wukong.configcenter.client.core.converter.DynamicConverter;
import com.netflix.config.*;
import org.apache.log4j.Logger;

import java.util.List;

/**
* 描  述: 初始化archaius 获取配置信息
* 创建时间: 2018/5/22
*/
public class HttpPullConfigScheduler {


    private static Logger logger = Logger.getLogger(HttpPullConfigScheduler.class);

    public static void loadConfigSource(int initialDelayMillis,
                                        int delayMillis, boolean ignoreDeletesFromSource,
                                        String defaultConfigDirectoryUrl, String serviceConfigDirectoryUrl) {
        long begin = System.currentTimeMillis();
        logger.info("--------加载动态调度器-------");
        logger.info("连接远端配置中心...");
        ///创建轮询调度器
        AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(
                initialDelayMillis, delayMillis, ignoreDeletesFromSource);
        ///设置数据源
        PolledConfigurationSource source = new HttpPolledConfigurationSource(defaultConfigDirectoryUrl, serviceConfigDirectoryUrl);
        ///创建动态的配置
        DynamicConfiguration dynamicConfig = new DynamicConfiguration(source,
                scheduler);
        DynamicPropertyFactory.initWithConfigurationSource(dynamicConfig);
        List sourceNames = HttpSourceConverter.getSourceNames(defaultConfigDirectoryUrl, serviceConfigDirectoryUrl);
        logger.info("--------加载动态调度器结束-------耗时="
                + (System.currentTimeMillis() - begin) + "ms");
        ///设置监听
        DynamicConverter.createCallBack(sourceNames);
    }
}
